from twisted.web import resource, server, static
from twisted.python import components
from twisted.internet import defer

from wbts.utils.web_utils import *
import os
class start(resource.Resource):
    def render_GET(self, request):
        request.setHeader("content-type", "text/html")
        # unfortunately we can't get client id for this test ;(
        client_id = 'unknown'
        
        data = request.channel.last_request
        output = "\r\n".join(data)
        output += "\r\n"+request.content.getvalue()
        log.msg(output)
        resp_data = """
        <html>
        <head>
        <title>Header Test redirect landing</title>
        <script type="text/javascript" src="/shared/scripts/testcase.js">
        </script>
        <script type="text/javascript">
        if (window.addEventListener) {
            window.addEventListener('load', f, false);
        } else if (window.attachEvent) {
            window.attachEvent('onload', f);
        } else {
            window.onload = f;
        }
        function f() {
            var tc = new TestCase();
            var output_data = document.getElementById("output_data").innerHTML;
            // make a fake XHR obj
            var xhr = new Object();
            xhr.responseText = output_data;
            
            tc.client_id = "%(client_id)s";
            tc.testcase_url = "%(testcase_url)s";
            tc.input = "%(input)s";
            tc.output = document.getElementById("output_data").innerHTML;
            tc.description = "%(description)s";
            tc.expected_result = "%(expected_result)s";
            tc_parsehdrs_for_crlf(xhr, tc);
        }
        </script>
        <body>
        <div id="output_data">%(output)s</div>
        </body>
        </html>
        """%({'client_id': client_id,
              'testcase_url': "http://attacker.com/testcases/headertests/header-injection-meta-refresh-1.html",
              'input': "&lt;meta http-equiv=\\\"refresh\\\" content=\\\"0;url=http://attacker.com/testcases/headertests/header-injection-meta-refresh-1.rpy+HTTP/1.0\\\r\\\nx-injheader: NewHeader;\\\"&gt;",
              'output': output,
              'description': 'Location with CR/LF injected (MetaTag \\r\\n)',
              'expected_result': "' ' or '_' or '-' or 'undefined'"
        })
        request.write(resp_data)
        request.finish()
        return server.NOT_DONE_YET
    
    render_POST = render_GET
    render_TRACE = render_GET
        
resource = start()
